package cn.tedu.hongbeifang.ex.handler;

import cn.tedu.hongbeifang.ex.ServiceException;
import cn.tedu.hongbeifang.web.JsonResult;
import cn.tedu.hongbeifang.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.Set;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler
    public JsonResult<Void> handleServiceException(ServiceException e) {
        return JsonResult.fail(e.getCode(), e.getMessage());
    }

    @ExceptionHandler
    public JsonResult<Void> handleAuthenticationException(AuthenticationException e) {
        log.debug("程序运行过程中出现AuthenticationException,将统一处理");
        log.debug("异常类型:{}",e.getClass().getName());
        log.debug("异常信息:{}",e.getMessage());
        String message = "登录失败,用户名或密码错误!";
        return JsonResult.fail(ServiceCode.ERROR_UNAUTHORIZED, message);
    }

    @ExceptionHandler
    public JsonResult<Void> handleDisabledException(DisabledException e) {
        log.debug("程序运行过程中出现DisabledException,将统一处理");
        log.debug("异常类型:{}",e.getClass().getName());
        log.debug("异常信息:{}",e.getMessage());
        String message = "登录失败,账号已经被禁用!";
        return JsonResult.fail(ServiceCode.ERROR_UNAUTHORIZED_DISABLED, message);
    }
    @ExceptionHandler
    public JsonResult<Void> handleAccessDeniedException(AccessDeniedException e) {
        log.debug("程序运行过程中出现AccessDeniedException,将统一处理");
        log.debug("异常类型:{}",e.getClass().getName());
        log.debug("异常信息:{}",e.getMessage());
        String message = "拒绝访问，您当前登录的账号无此操作权限！";
        return JsonResult.fail(ServiceCode.ERROR_FORBIDDEN, message);
    }

    @ExceptionHandler
    public JsonResult<Void> handleNullPointerException(NullPointerException e) {
        return JsonResult.fail(ServiceCode.ERROR, "你的程序出现了空指针异常，请检查代码！");
    }

    //局部变量不用考虑线程安全问题
    @ExceptionHandler
    public JsonResult<Void> handleBindException(BindException e){
//        StringBuilder stringBuilder = new StringBuilder();
//        List<FieldError> fieldErrors = e.getFieldErrors();
//        for (FieldError fieldError : fieldErrors) {
//            stringBuilder.append(fieldError.getDefaultMessage());
//        }
//        String message=stringBuilder.toString();
        String message=e.getFieldError().getDefaultMessage();
        return JsonResult.fail(ServiceCode.ERROR_BAD_REQUEST , message);
    }

    @ExceptionHandler
    public JsonResult<Void> handleConstraintViolationException(ConstraintViolationException e){
        StringBuilder stringBuilder = new StringBuilder();
        Set<ConstraintViolation<?>> constraintViolations = e.getConstraintViolations();
        for (ConstraintViolation<?> constraintViolation : constraintViolations) {
            stringBuilder.append(constraintViolation.getMessage());
        }
        String message=stringBuilder.toString();

        return JsonResult.fail(ServiceCode.ERROR_BAD_REQUEST , message);
    }


    @ExceptionHandler
    public JsonResult handleThrowable(Throwable e) {
        String message = "服务器忙，请稍后再次尝试！（开发过程中，如果看到此提示，请检查控制台的信息，并补充处理异常的方法）";
        e.printStackTrace(); // 注意：在生产环境中，禁止使用此语句！！！
        return JsonResult.fail(ServiceCode.ERROR, message);
    }

}
